const {
	wxml,
	style
} = require('@/wxcomponents/DomData.js');
import {
	mapMutations
} from 'vuex';
export default {
	...mapMutations([
		'upUserInfo'
	]),
	getPageList() {
		let that = this
		that.$httpRequest.get({
			that: that,
			url: 'wechat/getMyChangeList',
			params: {
				pageNo: that.page,
				pageSize: that.list_rows,
			},
			success: res => {
				that.loading = false
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					console.log(res.data)
					if (that.page === 1) {
						that.list = res.data.rows
					} else {
						that.list = that.list.concat(res.data.rows)
					}
					that.page++
				} else {
	
				}
			}
		})
	},
	toBalanceWithdraw() {
		this.$refs.withdrawPop.open()
	},
	withdrawNumber(e) {
		if (e.target.value > this.userInfo.balance) {
			this.$toast.msg('提现已经超过余额！', {
				icon: 1
			})
		} else {
			this.withdrawAmount = e.target.value
		}
	},
	cannelWithdraw() {
		this.$refs.withdrawPop.close()
	},
	balanceWithdraw() {
		let that = this
		if (that.withdrawAmount > that.userInfo.balance) {
			that.$toast.msg('提现已经超过余额！', {
				icon: 1
			})
			return false
		}
		that.$httpRequest.request({
			that,
			url: 'wechat/balanceWithdraw',
			params: {
				withdrawAmount: that.withdrawAmount
			},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.userInfo.balance = that.userInfo.balance - that.withdrawAmount
					that.cannelWithdraw()
				} else {
					that.$toast.alert({
						content: res.message,
						callback: result => {
							if (result.confirm) {

							}
						}
					})
				}
			}
		})
	},
	openMyCode() {
		this.show = !this.show
		if (this.show) {
			this.getMyShareCode()
		}
	},
	getPageData() {
		let that = this
		that.$httpRequest.get({
			that,
			url: 'wechat/getMyShare',
			params: {},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.detail = res.data
					that.shareCodeBg = res.data.myShareBackgroundPath
				} else {
					// that.$toast.alert({
					// 	content: res.message,
					// 	callback: result => {
					// 		if (result.confirm) {
					// 			uni.navigateBack({
					// 				delta: 1
					// 			})
					// 		}
					// 	}
					// })
				}
			}
		})
	},
	//升级需要支付
	toUpgrade() {
		let that = this
		that.$httpRequest.get({
			that,
			url: 'wechat/upgradeShareLevel',
			params: {},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					that.$toast.msg(res.message, {
						icon: 1
					})
					that.wechatPay(res.data)
				} else {
					that.$toast.alert({
						content: res.message,
						callback: result => {
							if (result.confirm) {
								uni.navigateBack({
									delta: 1
								})
							}
						}
					})
				}
			}
		})
	},
	wechatPay(id) {
		let that = this

		if (that.loading) {
			that.$toast.msg('请求中...', {
				icon: 1
			})
			return false
		}
		that.loading = true
		uni.requestSubscribeMessage({
			tmplIds: ['Vc2tzjTdqB3dBLxTOwgJsECSpSeh20rtdCKfelFCnfY'],
			success(res) {
				let url = 'wechat/orderPay'
				that.$httpRequest.request({
					that: that,
					url,
					params: {
						id: id
					},
					success: res => {
						that.loading = false
						if (res.code === that.$httpConfig.SUCCESS_CODE) {
							// that.upSuccessPay(true)
							let data = res.data
							uni.requestPayment({
								timeStamp: data.timeStamp,
								nonceStr: data.nonceStr,
								package: data.package,
								signType: data.signType,
								paySign: data.paySign,
								success: res => {
									that.$toast.alert({
										content: '支付成功！',
										callback: result => {
											if (result.confirm) {
												uni.navigateBack({
													delta: 1
												})
											}
										}
									})
								}
							})
						} else {
							that.$toast.alert({
								content: res.msg,
								callback: result => {

								}
							})
						}
					}
				})
			}
		});

	},
	getMyShareCode() {
		uni.showLoading({
			title: '海报生成中...'
		})
		let that = this
		that.$httpRequest.get({
			that,
			url: 'wechat/getMyShareCode',
			params: {},
			success: res => {
				if (res.code === that.$httpConfig.SUCCESS_CODE) {
					//二维码
					that.shareCode = res
						.data //'https://pic1.zhimg.com/80/v2-58fe538a59f870407b1435bfd45893ed_720w.jpeg' //需要https协议
					that.shareCodeBg = that.shareCodeBg
					setTimeout(() => { //调用生成海报
						this.widget = this.selectComponent('.widget')
						this.renderToCanvas()
					}, 300) //必须用定时器
				} else {
					uni.hideLoading()
					that.$toast.alert({
						content: res.message,
						callback: result => {
							if (result.confirm) {
								uni.navigateBack({
									delta: 1
								})
							}
						}
					})
				}
			}
		})
	},

	toBillDetail() {
		uni.navigateTo({
			url: '/pages/my/billDetail'
		})
	},
	toMyBusiness() {
		uni.navigateTo({
			url: '/pages/my/myBusiness'
		})
	},

	// wxml 转 canvas
	renderToCanvas() {
		// console.log(this.userInfo.userName, this.userInfo.avatar, this.shareCode,this.shareCodeBg)
		const _wxml = wxml(this.userInfo.userName, this.userInfo.avatar, this.shareCode, this
			.shareCodeBg) //用户名称，用户头像，背景图,分享二维码
		const _style = style(this.screenWidth, this.canvasWidth, this.canvasHeight)
		const p1 = this.widget.renderToCanvas({
			wxml: _wxml,
			style: _style
		})
		p1.then((res) => {
			console.log('海报生成成功');
			uni.hideLoading()
			// this.container = res
		}).catch((err) => {
			uni.hideLoading()
			console.log('生成失败')
		})
	},
	// 保存到朋友圈
	extraImage() {
		if (!this.show) {
			wx.showToast({
				title: '海报生成失败，无法分享到朋友圈',
				icon: 'none'
			})
			return
		}
		const p2 = this.widget.canvasToTempFilePath()
		let that = this
		p2.then(result => {
			let path = result.tempFilePath
			wx.getSetting({
				success: res => {
					// 非初始化且未授权的情况，需要再次弹窗提示授权
					if (res.authSetting['scope.writePhotosAlbum'] != undefined && res.authSetting[
							'scope.writePhotosAlbum'] != true) {
						wx.showModal({
							title: '是否授权相册权限',
							content: '需要获取相册权限，请确认授权，否则无法使用相关功能',
							success: res => {
								if (res.confirm) {
									wx.openSetting({
										success: dataAu => {
											if (dataAu.authSetting[
													"scope.writePhotosAlbum"
												] == true) {
												wx.showToast({
													title: '授权成功',
													icon: 'none',
													duration: 1000
												});
												that.saveIMg(path);
											} else {
												wx.showToast({
													title: '授权失败',
													icon: 'success',
													duration: 1000
												});
											}
										}
									});
								}
							}
						});
					} else {
						// 初始化且未授权，系统默认会弹窗提示授权
						// 非初始化且已授权，也会进入这里
						that.saveIMg(path);
					}
				}
			});
		})
	},
	// 保存到相册
	async saveIMg(tempFilePath) {
		wx.saveImageToPhotosAlbum({
			filePath: tempFilePath,
			success: async (res) => {
				wx.showModal({
					content: '图片已保存，分享给好友吧！',
					showCancel: false,
					confirmText: '好的',
					confirmColor: '#333',
					success: function(res) {
						wx.navigateBack({
							//返回
							delta: 1
						});
					},
					fail: function(res) {
						console.log('res', res);
					}
				});
			},

			fail: function(res) {
				wx.showToast({
					title: '您取消了授权',
					icon: 'none',
					duration: 2000
				})
			}
		});
	}
}